package cn.jnetty.ioc.filter;
import cn.jnetty.api.annotation.JPostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @author 猪哥亮
 * @email 1030681978@qq.com
 * @date 2020/3/13 11:01
 * 描述：过滤器
 */
public abstract class AbstractJnettyFilter {
    private Logger log = LoggerFactory.getLogger(AbstractJnettyFilter.class);
    private final Set<String> filterSet = new HashSet<>();

    public AbstractJnettyFilter() {
        addFilters();
    }

    protected void addFilters(String...ids){
        filterSet.addAll(Arrays.asList(ids));
    }
    public AbstractJnettyFilter addFilter(String id){
        filterSet.add(id);
        log.debug("添加拦截器的Id------------------{}",id);
        return this;
    }
    public void removeFilters(String...ids){
        filterSet.removeAll(Arrays.asList(ids));
    }
    public AbstractJnettyFilter removeFilter(String id){
        filterSet.remove(id);
        log.debug("删除拦截器的Id------------------{}",id);
        return this;
    }

    /**
     * 是否需要过滤
     * @param id 过滤的id
     * @return
     */
    public boolean isFilter(String id){
        return filterSet.contains(id);
    }

    /**
     * 添加过滤器
     */
    @JPostConstruct
    public abstract void addFilters();
}
